import { ValidatorResponse } from "@/Types";
import { IValidator } from "./IValidator";

/**
 * 验证字符串长度为指定值
 *
 * @param fixedLength 指定的最小长度
 * @param err 验证失败时输出的字符串
 *
 * @exmaple
 * FixedLengthValidator(10) // 验证错误时会输出 `输入长度必须为10`
 */
export const FixedLengthValidator = function(
  fixedLength: number,
  err = `输入长度必须为${fixedLength}`
): IValidator<string> {
  return function(input: string): ValidatorResponse {
    if (input.length != fixedLength) {
      return err;
    }
    return true;
  };
};
